//
//  AppDelegate.m
//  UI17_NSFileManager
//
//  Created by dllo on 16/8/5.
//  Copyright © 2016年 yh. All rights reserved.
//

#import "AppDelegate.h"

@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    // 创建沙盒
//    实现文件、文件夹的基本操作(调研NSFileManager, 并实现以下功能)
//    创建文件、文件夹
//    删除文件、文件夹
//    文件 是否存在
//    移动文件
//    拷贝文件
    
    // 创建沙盒
    NSArray *sandBoxArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    // 找到沙盒路径
    NSString *path = [sandBoxArray firstObject];
    NSLog(@"%@", path);
    // 创建文件管理器
    NSFileManager *file = [NSFileManager defaultManager];
    // 根据沙盒路径 创建文件夹
    NSString *filePath = [path stringByAppendingPathComponent:@"test"];
    NSString *secFilePath = [path stringByAppendingPathComponent:@"secTest"];
    NSLog(@"%@", secFilePath);
    // 创建目录
    [file createDirectoryAtPath:filePath withIntermediateDirectories:YES attributes:nil error:nil];
    [file createDirectoryAtPath:secFilePath withIntermediateDirectories:YES attributes:nil error:nil];
    // 创建文件
    NSString *test1 = [secFilePath stringByAppendingPathComponent:@"test11"];
    // 创建文件内容
    NSString *str = @"Eason";
    // 文件管理器调用方法 将文件内容添加到文件中
    [file createFileAtPath:test1 contents:[str dataUsingEncoding:NSUTF8StringEncoding] attributes:nil];
    NSString *test2 = [filePath stringByAppendingPathComponent:@"test12"];
    NSString *str1 = @"Andy";
    [file createFileAtPath:test2 contents:[str1 dataUsingEncoding:NSUTF8StringEncoding] attributes:nil];
    
    NSString *secTest = [secFilePath stringByAppendingPathComponent:@"secTest1"];
    NSString *secStr = @"Edition";
    [file createFileAtPath:secTest contents:[secStr dataUsingEncoding:NSUTF8StringEncoding] attributes:nil];
    // 删除文件(删除test11文件 只需要填一个test11文件的路径text1就可以)
//    // 删除文件夹(只需要填一个test文件夹的路径就可以了)
//    [file removeItemAtPath:filePath error:nil];
    // 判断文件或者文件夹是否存在
#if 0
    NSString *readStr = [NSString stringWithContentsOfFile:test1 encoding:NSUTF8StringEncoding error:nil];
    if (readStr != nil) {
        NSLog(@"文件test11存在");
    } else {
        NSLog(@"文件test11不存在");
    }
#endif
    if ([file fileExistsAtPath:test1]) {
        NSLog(@"存在");
    } else {
        NSLog(@"不存在");
    }
    // 移动文件
    [file moveItemAtPath:test1 toPath:[filePath stringByAppendingPathComponent:@"asd"] error:nil];
    // 复制文件
    BOOL isSrc = [file copyItemAtPath:test2 toPath:[secFilePath stringByAppendingString:@"/sss"] error:nil];
    if (isSrc) {
        NSLog(@"成功");
    } else {
        NSLog(@"失败");
    }
    
    
    return YES;
}

- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end
